package com.synjones.handsetS8.fp;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.synjones.handsetS8.R;;
import com.synjones.handsetS8.Utils.util_File;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class SetTemplateActivity extends Activity {
	public static List<ListDataItem> listData=new ArrayList<ListDataItem>();
	public static final String KEY_TEMPLATE_NAME="key_template_name";
	ListView templateList;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.template_list_view);
		templateList=(ListView) findViewById(R.id.lvTemplates);
		TemplateListAdapter adapter=new TemplateListAdapter(this);
		templateList.setAdapter(adapter);
		Button btn=(Button)findViewById(R.id.btnClearModuleTemplate);
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				FPTestActivity.fpModule.open();
				int ret= FPTestActivity.fpModule.cleanFpFeature();
				if(ret==0){
					Toast.makeText(SetTemplateActivity.this, "清空模块内模板成功", Toast.LENGTH_SHORT).show();
				}
				else
					Toast.makeText(SetTemplateActivity.this, "清空模块内模板失败ret="+ret, Toast.LENGTH_LONG).show();
				FPTestActivity.fpModule.close();
			}
		});
		btn=(Button)findViewById(R.id.btnClearList);
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				clearList();
				((BaseAdapter)templateList.getAdapter()).notifyDataSetChanged();
			}
		});
		btn=(Button)findViewById(R.id.btnBack);
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		btn=(Button)findViewById(R.id.btnAddTestData);
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				addTestData2List();
			}
		});

	}

	public void addTestData2List(){
		File dir=new File(FPTestActivity.FP_DATA_PATH);
		File files[] = dir.listFiles();
		if(files==null || files.length<=0){
			Toast.makeText(SetTemplateActivity.this, "没有测试数据", Toast.LENGTH_SHORT).show();
			return;
		}
		for(File f:files)
		{
			String name=f.getName();
			if(name.endsWith(".dat")){
				ListDataItem item=new ListDataItem();
				item.id=getListSize()+1;
				item.img=null;
				item.info=name.substring(0,name.lastIndexOf("."));
				item.template= util_File.readAllSdcardFile(FPTestActivity.FP_DATA_PATH+name);;
				addTemplate2List(item);
			}
		}
		((BaseAdapter)templateList.getAdapter()).notifyDataSetChanged();
	}

	public static void addTemplate2List(ListDataItem item){
		listData.add(item);
	}

	public static void clearList(){
		listData.clear();
	}

	public static int getListSize(){
		return listData.size();
	}

	static class ViewHolder
	{
		public ImageView img;
		public TextView info;
		public Button btn;
	}



	class TemplateListAdapter extends BaseAdapter{

		private LayoutInflater mInflater = null;
		private TemplateListAdapter(Context context)
		{
			//根据context上下文加载布局，这里的是Demo17Activity本身，即this
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return listData.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return listData.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			//如果缓存convertView为空，则需要创建View
			if(convertView == null)
			{
				holder = new ViewHolder();
				//根据自定义的Item布局加载布局
				convertView = mInflater.inflate(R.layout.template_item_view, null);
				holder.img = (ImageView)convertView.findViewById(R.id.ivFingerImg);
				holder.info = (TextView)convertView.findViewById(R.id.tvTemplateInfo);
				holder.btn = (Button)convertView.findViewById(R.id.btnDownloadTemplate);
				//将设置好的布局保存到缓存中，并将其设置在Tag里，以便后面方便取出Tag
				convertView.setTag(holder);
			}else
			{
				holder = (ViewHolder)convertView.getTag();
			}
			ListDataItem item= listData.get(position);
			if(item.img!=null)
				holder.img.setImageBitmap(item.img);
			else
				holder.img.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.finger));
			String strIsDownload="未下载";
			if(item.isDownload)
				strIsDownload="已下载";
			holder.info.setText(item.info+"\r\nid="+item.id+","+strIsDownload);
			final int p=position;
			holder.btn.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					//FPTestActivity.fpModule.open();
					ListDataItem item= listData.get(p);
					int ret=FPTestActivity.fpModule.setFpFeature(item.id,item.template);
					if(ret==0){
						Toast.makeText(SetTemplateActivity.this, "下载成功,点击对比按钮开始比对", Toast.LENGTH_SHORT).show();
						listData.get(p).isDownload=true;
						((BaseAdapter)templateList.getAdapter()).notifyDataSetChanged();
					}
					else
						Toast.makeText(SetTemplateActivity.this, "下载失败ret="+ret, Toast.LENGTH_LONG).show();
					//FPTestActivity.fpModule.close();
					if(ret==0) {
						Intent intent=new Intent();
						intent.putExtra(KEY_TEMPLATE_NAME,item.info);
						setResult(RESULT_OK,intent);
						finish();
					}

				}
			});

			return convertView;
		}

	}



}
